package com.alks.function.service.impl.biocontrol.deptwork;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.entity.data.entity.PcLotHead;
import com.alks.entity.data.entity.PcLotMtlPrepair;
import com.alks.entity.data.enums.biocontrol.PrepairFlowEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.controller.finance.costcheck.LotMtlUsgPriceController;
import com.alks.function.data.request.biocontrol.deptwork.PrepairRequest;
import com.alks.function.data.request.biocontrol.deptwork.PrepairTimeRequest;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_PcLotMtlPrepairMapper;
import com.alks.function.mapper.chengKong.PcLotHeadMapper;
import com.alks.function.service.biocontrol.deptwork.IBefPrepairService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class BefPrepairServiceImpl extends ServiceImpl<BC_DW_PcLotMtlPrepairMapper, PcLotMtlPrepair> implements IBefPrepairService {
    private final BC_DW_PcLotMtlPrepairMapper prepairMapper;
    private final PcLotHeadMapper lotHeadMapper;
    private final LotMtlUsgPriceController lot;

    @Override
    @AutoPageAop
    public ResponseInfo getBefPrepairQuery(PrepairRequest request) {
        /*入参处理*/
        if(request.getFlow()==null){
            request.setFlag(null);
        }
        request.setFlow(PrepairFlowEnum.getColumnByName(request.getFlow()));
        /*获取全部数据*/
        List<PcLotMtlPrepair> dtos = prepairMapper.getBefPrepairQuery(request, UserIdThread.get().getCompanyId());
        /*封装数据*/
        Map<String,Object> map=new HashMap<>();
        map.put("dtos",dtos);
        if (request.getPageSize()!=null&&request.getPageSize()!=-1) {
            Page<PcLotMtlPrepair> page = (Page<PcLotMtlPrepair>) dtos;
            map.put("total", page.getTotal());
        }
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo befPrepairUp(Integer flag) {
        String companyId = UserIdThread.get().getCompanyId();
        int lotUp = 1;
        int dataUp = 2;
        if (flag.equals(lotUp)){
            /*获取已有指令集*/
            List<String> lotNoList = lambdaQuery()
                    .select(PcLotMtlPrepair::getLotNo)
                    .eq(PcLotMtlPrepair::getCompanyId,companyId)
                    .groupBy(PcLotMtlPrepair::getLotNo)
                    .list().stream()
                    .map(PcLotMtlPrepair::getLotNo)
                    .collect(Collectors.toList());
            /*寻找缺失数据*/
            LambdaQueryWrapper<PcLotHead> lotWrapper = new LambdaQueryWrapper<PcLotHead>()
                    .select(PcLotHead::getCompanyId,PcLotHead::getLotNo,PcLotHead::getCustom,
                            PcLotHead::getStyle,PcLotHead::getColor,PcLotHead::getOrderQty,PcLotHead::getSurplusQty,
                            PcLotHead::getLastNo,PcLotHead::getOrderDate,PcLotHead::getDeliveryDate)
                    .eq(PcLotHead::getCompanyId,companyId)
                    .eq(PcLotHead::getCfmFlag,"Y")
                    .notIn(PcLotHead::getLotNo, lotNoList);
            List<PcLotHead> lotHeadList = lotHeadMapper.selectList(lotWrapper);
            if (ArrayUtils.isEmpyt(lotHeadList)){
                return ResponseInfo.ok();
            }
            for (PcLotHead lotHead : lotHeadList) {
                /*写入新数据*/
                PcLotMtlPrepair prepair = new PcLotMtlPrepair();
                prepair.setCompanyId(lotHead.getCompanyId());
                prepair.setLotNo(lotHead.getLotNo());
                prepair.setCustom(lotHead.getCustom());
                prepair.setStyle(lotHead.getStyle());prepair.setStyle(lotHead.getStyle());
                prepair.setColor(lotHead.getColor());
                BigDecimal orederQty =lotHead.getOrderQty()==null?BigDecimal.ZERO:BigDecimal.valueOf(lotHead.getOrderQty());
                prepair.setOrderQty(orederQty);
                prepair.setLastNo(lotHead.getLastNo());
                prepair.setOrderDate(lotHead.getOrderDate());
                prepair.setTechBomAct(lotHead.getOrderDate());
                prepair.setDeliveryDate(lotHead.getDeliveryDate());
                LocalDateTime planDate = lotHead.getOrderDate()==null?LocalDateTime.now().plusDays(6):lotHead.getOrderDate().plusDays(6);
                prepair.setUpperPurPlan(planDate);
                prepair.setSolePurPlan(planDate);
                /*评审时间固定七天*/
                prepair.setHfLt(BigDecimal.valueOf(7));
                prepairMapper.insert(prepair);
            }
        }else if (flag.equals(dataUp)){
            /*数据更新*/

        }
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo befPrepairTimeUp(PrepairTimeRequest request) {
        /*更新数据*/
        lambdaUpdate().eq(PcLotMtlPrepair::getLotNo, request.getLotNo())
                .set(PcLotMtlPrepair::getBussAct, request.getBussAct())
                .set(PcLotMtlPrepair::getTechBomAct, request.getTechBomAct())
                .set(PcLotMtlPrepair::getTechTrailAct, request.getTechTrailAct())
                .set(PcLotMtlPrepair::getUpperPurAct, request.getUpperPurAct())
                .set(PcLotMtlPrepair::getSolePurAct, request.getSolePurAct())
                .set(PcLotMtlPrepair::getUpperMtlVendor, request.getUpperMtlVendor())
                .set(PcLotMtlPrepair::getUpperMtlPmc, request.getUpperMtlPmc())
                .set(PcLotMtlPrepair::getUpperMtlAct, request.getUpperMtlAct())
                .set(PcLotMtlPrepair::getSoleMtlVendor, request.getSoleMtlVendor())
                .set(PcLotMtlPrepair::getSoleMtlPmc, request.getSoleMtlPmc())
                .set(PcLotMtlPrepair::getPackMtlAct, request.getPackMtlAct())
                .update();
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getBefCustom() {
        /*获取数据*/
        List<String> dtos = lambdaQuery().groupBy(PcLotMtlPrepair::getCustom).list()
                .stream().map(PcLotMtlPrepair::getCustom).collect(Collectors.toList());
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getBefStyle() {
        List<String> dtos = lambdaQuery().groupBy(PcLotMtlPrepair::getStyle).list()
                .stream().map(PcLotMtlPrepair::getStyle).collect(Collectors.toList());
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getBefLast() {
        List<String> dtos = lambdaQuery().groupBy(PcLotMtlPrepair::getLastNo).list()
                .stream().map(PcLotMtlPrepair::getLastNo).collect(Collectors.toList());
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo getBefPrepair() {
        List<String> dtos = PrepairFlowEnum.getNameList();
        return ResponseInfo.ok(dtos);
    }

}
